package com.tubruk.trackme;

import java.util.ArrayList;
import java.util.List;

import com.tubruk.adapter.TrackSummaryAdapter;
import com.tubruk.database.TrackSummaryDB;
import com.tubruk.object.TrackSummary;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class History extends Activity {
	private List<TrackSummary> items;
	private List<TrackSummary> all;
	private TrackSummaryAdapter adapter;
	
	private ListView historyList;
	
	private static final int SUMMARY = 1;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.history);
        		
		ImageButton locate = (ImageButton) findViewById(R.id.locate);
		locate.setOnClickListener(new LocateClickListener());
		
		EditText et = (EditText) findViewById(R.id.searchtext);
		et.addTextChangedListener(new SearchTextWatcher());

		all = TrackSummaryDB.getAll();
		items = new ArrayList<TrackSummary>();
		items.addAll(all);
		adapter = new TrackSummaryAdapter(this, R.id.historyList, items);
		
		//bind list
		historyList = (ListView) this.findViewById(R.id.historyList);
		historyList.setAdapter(adapter);
		
		historyList.setOnItemClickListener( new ItemListener());
		
	}
	
	private class SearchTextWatcher implements TextWatcher {

		@Override
		public void afterTextChanged(Editable arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onTextChanged(CharSequence s, int start, int before,
				int count) {
			// TODO Auto-generated method stub
			//FILTER
			if (all != null){
				adapter.clear();
				int size = all.size();
				for (int i=0;i<size;i++){
					TrackSummary sum = all.get(i);
					String title = sum.getTitle();
					if (title != null && title.toLowerCase().indexOf(s.toString().toLowerCase()) > -1){
						adapter.add(sum);
					}
				}
			}
		}
		
	}
	
	private class ItemListener implements OnItemClickListener {
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			int selectionRowID = (int) historyList.getSelectedItemId();
			
			Log.d("SELECTED: ", selectionRowID + "");
		
			TrackSummary sum = (TrackSummary) historyList.getItemAtPosition(selectionRowID);
			
			Intent i = new Intent(History.this, Summary.class);
			i.putExtra(Summary.OBJECT_SELECTED, sum);
			
			startActivityForResult(i, History.SUMMARY);
			
		}
	}
	
	@Override 
	public void onActivityResult(int requestCode, int resultCode, Intent data) {     
	  super.onActivityResult(requestCode, resultCode, data); 
	  switch(requestCode) { 
	    case (History.SUMMARY) : { 
	    	
	      if (resultCode == Activity.RESULT_OK) {
	    	  Bundle extras = data.getExtras();
	    	  if (extras != null){
	    		  Object obj = extras.get(Summary.OBJECT_SELECTED);
	    		  TrackSummary sum = null;
	    		  if (obj != null){
	    			  sum = (TrackSummary) obj;
	    		  }
	    		  
	    		  int action = extras.getInt(Summary.ACTION_SELECTED);
	    		  if (action == Summary.MENU_SAVE){
	    			  int result = TrackSummaryDB.update(sum);
	    			  if (result > -1){
	    				  int size = adapter.getCount();
	    				  for (int i = 0;i<size;i++){
	    					  TrackSummary temp = adapter.getItem(i);
	    					  if (temp.getId() == sum.getId()){
	    						  temp.setTitle(sum.getTitle());
	    						  i = size;
	    						  adapter.notifyDataSetChanged();
	    					  }
	    				  }
	    				  
	    				  
	    				  size = all.size();
	    				  for (int i=0;i<size;i++){
	    					  TrackSummary temp = adapter.getItem(i);
	    					  if (temp.getId() == sum.getId()){
	    						  temp.setTitle(sum.getTitle());
	    						  i = size;
	    					  }
	    				  }
	    			  }
	    		  }else if (action == Summary.MENU_DELETE){
	    			  Log.d("HOLA", "6");
	    			  int result = TrackSummaryDB.delete(sum.getId());
	    			  
	    			  if (result > -1){
	    				  Log.d("HOLA", "7");
		    			  int size = adapter.getCount();
	    				  for (int i = 0;i<size;i++){
	    					  TrackSummary temp = adapter.getItem(i);
	    					  if (temp.getId() == sum.getId()){
	    						  adapter.remove(adapter.getItem(i));
	    						  i = size;
	    					  }
	    				  }
	    				  
	    				  size = all.size();
	    				  for (int i=0;i<size;i++){
	    					  TrackSummary temp = all.get(i);
	    					  if (temp.getId() == sum.getId()){
	    						  all.remove(i);
	    						  i = size;
	    					  }
	    				  }
	    			  }
	    		  }
	    	  }
	      }
	    }
	  }
	}
	
	private class LocateClickListener implements OnClickListener {

		public void onClick(View v) {
			finish();
			Intent i = new Intent( History.this, TrackMapActivity.class);
			startActivity(i);
		}
		
	}
}
